import { getFile } from '@/utils/fileutils'
import request from '@/utils/request'

const baseURL = '/user'

// 保存用户信息
export const saveUser = (userInfo) => request.post(`${baseURL}/save`, userInfo)

// 保存密保问题
export const saveAnswer = (question) => request.post(`${baseURL}/saveQuestion`, question)

// 获取用户密保问题
export const getQuestion = (id) =>
  request.get(`${baseURL}/getQuestion`, {
    params: {
      id,
    },
  })

// 获取用户基本信息
export const getBaseInfo = () => request.get(`${baseURL}/getBaseInfo`)

// 更新用户信息
export const updateUser = (data) => request.post(`${baseURL}/updateUser`, data)

// 获取用户头像
export const getAvatar = async () => {
  const data = await request.get(`${baseURL}/getAvatar`, {
    headers: {
      Authorization: localStorage.getItem('accessToken')
    }
  })

  const { data: { data: file } } = data
  // 根据路径获取文件
  return getFile(file)
}

// 根据id获取用户头像
export const getAvatarById = async (id) => {
  const file = (
    await request.get(`${baseURL}/getAvatarById`, {
      params: {
        id,
      },
    })
  ).data.data
  return getFile(file)
}

// 更新用户头像
export const updateAvatarURL = (data) => {
  return request.post(`${baseURL}/saveAvatar`, data, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

// 获取余额和积分
export const getBalanceIntegral = () => request.get(`${baseURL}/getBalanceIntegral`)

// 更新余额和积分
export const updateBalanceIntegral = (data) =>
  request.post(`${baseURL}/updateBalanceIntegral`, data)

// 充值
export const addMoneyAndIntegral = (data) =>
  request.get(`${baseURL}/addMoneyAndIntegral`, {
    params: {
      id: '',
      money: +data.balance,
      integral: +data.integral,
    },
  })

// 获取充值验证码
export const getBalanceVerificationCode = (money) =>
  request.get(`${baseURL}/getBalanceVerificationCode`, {
    params: {
      money,
    },
  })

// 校验充值验证码
export const verifyBalanceVerificationCode = (code) =>
  request.get(`${baseURL}/verifyBalanceVerificationCode`, {
    params: {
      code,
    },
  })
